//
//  EasyToTescoNewsDetailsVc.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/9/30.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "EasyToTescoNewsDetailsVc.h"
#import <WebKit/WebKit.h>

@interface EasyToTescoNewsDetailsVc ()<WKNavigationDelegate>
{
    MBProgressHUD *_progressHUD;
    NSString *_urlStr;
}

@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, assign) NSUInteger loadCount;
@property(nonatomic,strong) WKWebView *wkWebView;

@end

@implementation EasyToTescoNewsDetailsVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:15],NSFontAttributeName, nil] forState:UIControlStateNormal];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)popViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setLink:(NSString *)link
{

    
    
    //活动列表push
    if (_is_actPush) {
       
        _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT-64)];
        [_wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:link]]];
        [self.view addSubview:_wkWebView];
        _wkWebView.navigationDelegate = self;
        return;
    }
    
    
    
    //可为快报详情
    NSDictionary *params = @{
                             @"id":link
                             };
    NSLog(@"%@",params);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/News/Info" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            NSDictionary *dataDict = [object objectForKey:@"data"];
            _urlStr = [dataDict objectForKey:@"content"];
            
            
            if (!_wkWebView) {
                _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0,0, WIDTH,HEIGHT-64)];
                [_wkWebView loadHTMLString:_urlStr baseURL:nil];
                [self.view addSubview:_wkWebView];
                _wkWebView.navigationDelegate = self;
                [_wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
            }
            
            
            if (!_progressView) {
                _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0,0,WIDTH, 0)];
                [_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
                _progressView.progressTintColor = DEFAULT_COLOR;
                _progressView.transform = CGAffineTransformMakeScale(1.0f,3.0f);
                [self.view addSubview:_progressView];
            }

            
        }
        
    }];


}

- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context
{
    
    if ([keyPath isEqualToString:@"loading"]) {
        
    } else if ([keyPath isEqualToString:@"title"]) {
        self.title = _wkWebView.title;
    } else if ([keyPath isEqualToString:@"URL"]) {
        
    } else if ([keyPath isEqualToString:@"estimatedProgress"]) {
        
        self.progressView.progress = _wkWebView.estimatedProgress;
    }
    
    if (object == _wkWebView && [keyPath isEqualToString:@"estimatedProgress"]) {
        CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
        if (newprogress == 1) {
            self.progressView.hidden = YES;
            [self.progressView setProgress:0 animated:NO];
        }else {
            self.progressView.hidden = NO;
            [self.progressView setProgress:newprogress animated:YES];
        }
    }
}

- (void)setLoadCount:(NSUInteger)loadCount {
    _loadCount = loadCount;
    
    if (loadCount == 0) {
        self.progressView.hidden = YES;
        [self.progressView setProgress:0 animated:NO];
    }else {
        self.progressView.hidden = NO;
        CGFloat oldP = self.progressView.progress;
        CGFloat newP = (1.0 - oldP) / (loadCount + 1) + oldP;
        if (newP > 0.95) {
            newP = 0.95;
        }
        [self.progressView setProgress:newP animated:YES];
        
    }
}


// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    //菊花加载
    self.loadCount ++;
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
    self.loadCount --;
}
// 页面加载完成之后调用
//- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
//{
//   self.loadCount --;
//}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    
    self.loadCount --;
    //弹框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"页面加载失败" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    
    [alert addAction:cancle];
    [self presentViewController:alert animated:YES completion:nil];
}

// 记得取消监听
- (void)dealloc {

    [self.wkWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}



- (void)viewWillAppear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    [self.navigationController.navigationBar setBarTintColor:DEFAULT_COLOR];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

@end
